﻿using System;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Routing;
using System.Web.Mvc;

namespace TidyBlog.MVC
{
    /// <summary>
    /// 操作提示类
    /// </summary>
    public abstract class RespondentInfo
    {
        /// <summary>
        /// 判断控制器名是否后台管理页
        /// </summary>
        protected static readonly Regex IsManage = new Regex(@"^Manage(_|$)");


        /// <summary>
        /// 呈现操作信息页
        /// </summary>
        /// <param name="context">控制器上下文</param>
        /// <param name="action">动作</param>
        protected static void Render(ControllerContext context, string action, OperInfo info)
        {
            var controllerName = "Info";
            if (IsManage.IsMatch(context.RouteData.Values["controller"].ToString()))
            {
                controllerName = "Manage_" + controllerName;
            }
            
            var viewResult = new ViewResult() {
                ViewName = "~/Views/" + controllerName + "/" + action + ".cshtml",
                ViewData = context.Controller.ViewData
            };
            viewResult.ViewData.Model = info;
            viewResult.ExecuteResult(context);

            context.HttpContext.Response.End();
        }

        /// <summary>
        /// 呈现错误信息提示页
        /// </summary>
        /// <param name="context">控制器上下文</param>
        /// <param name="info">操作信息</param>
        public static void Error(ControllerContext context, OperInfo info)
        {
            Render(context, "Error", info);
        }

        /// <summary>
        /// 呈现操作成功提示页
        /// </summary>
        /// <param name="context">控制器上下文</param>
        /// <param name="info">操作信息</param>
        public static void Success(ControllerContext context, OperInfo info)
        {
            Render(context, "Success", info);
        }

        /// <summary>
        /// 呈现操作提示页
        /// </summary>
        /// <param name="context">控制器上下文</param>
        /// <param name="info">操作信息</param>
        public static void Tips(ControllerContext context, OperInfo info)
        {
            Render(context, "Tips", info);
        }
    }
}